#!/usr/bin/python
# -*- coding:UTF-8 -*-

from schema import *
from xml.dom import minidom
from xml.dom import Node

import profilefield
##
# Xiaonei API
##

def getFriends(doc):
    stripEmptyTextNodes(doc)
    friends=[]
    childern=doc.firstChild.childNodes
    for child in childern:
        id=child.childNodes[0].firstChild.data
        name=child.childNodes[1].firstChild.data
        headurl=child.childNodes[2].firstChild.data
        f=Friend(id,name,headurl)
        friends.append(f)
    return friends

def getFriendIds(doc):
    stripEmptyTextNodes(doc)
    ids=[]
    children=doc.firstChild.childNodes
    for child in children:
        id=child.firstChild.data
        ids.append(id)
    return ids

def getLoggedInUser(doc):
    stripEmptyTextNodes(doc)
    return doc.firstChild.firstChild.data

def getAppUsers(doc):
    stripEmptyTextNodes(doc)
    users=[]
    children=doc.firstChild.childNodes
    for child in children:
        id=child.firstChild.data
        users.append(id)
    return users

def areFriends(doc):
    stripEmptyTextNodes(doc)
    r=[]
    children=doc.firstChild.childNodes
    for child in children:
        id1=child.childNodes[0].firstChild.data
        id2=child.childNodes[1].firstChild.data
        isf=child.childNodes[2].firstChild.data
        r.append((id1,id2,isf))
    return r


def isAppAdded(doc):
    stripEmptyTextNodes(doc)
    return doc.firstChild.firstChild.data

# return: a List of User
def getUserInfo(doc,fields):
    stripEmptyTextNodes(doc)
    r=[]
    builder=buildUser()
    children=doc.firstChild.childNodes
    for child in children:
        #print child.nodeName
        u=User()
        for field in fields:
            tag=child.getElementsByTagName(field.fieldName)
            if tag.length>0:
                #assert tag.length==1
                attr=getattr(builder,field.fieldName)
                assert callable(attr)
                u[field.fieldName]=attr(tag[0])
        r.append(u)
    return r
##
#utility functions
##
def stripEmptyTextNodes(n):
    children=n.childNodes
    for child in children:
        if (not child.hasChildNodes()) and child.nodeType==Node.TEXT_NODE and len(child.data.strip())==0:
            children.remove(child)
    for child in children:
        stripEmptyTextNodes(child)

            
def printDOM(n,prefix=""):
    outString=prefix
    if n.nodeType==Node.TEXT_NODE:
        outString+="["+n.data.strip()+"]"
    else:
        outString+=n.nodeName
    print outString
    children=n.childNodes
    for child in children:
        printDOM(child,prefix+"*")

class buildUser(object):
    def uid(self,doc):
        return doc.firstChild.data
    def name(self,doc):
        return doc.firstChild.data
    def sex(self,doc):
        return doc.firstChild.data
    def birthday(self,doc):
        return doc.firstChild.data
    def tinyurl(self,doc):
        return doc.firstChild.data
    def headurl(self,doc):
        return doc.firstChild.data
    def mainurl(self,doc):
        return doc.firstChild.data
    def hometown_location(self,doc):
        hl=HometownLocation()
        try:
            hl.province=doc.childNodes[0].firstChild.data
            hl.city=doc.childNodes[1].firstChild.data
        except:
            pass
        return hl
    def testcode1():
    p=None
    p=0
    if p is None:
        print "P is none"
    
    l=[1,2,3,6,4,3,5,1]
    s=set(l)
    print s
    l=list(s)
    print l

def test():
    xmldoc=minidom.parse("test/getLoggedInUser.xml")
    #print getLoggedInUser(xmldoc)
    xmldoc=minidom.parse("test/getAppUsers.xml")
    #print getAppUsers(xmldoc)
    xmldoc=minidom.parse("test/areFriends.xml")
    #print areFriends(xmldoc)
    xmldoc=minidom.parse("test/friends.get.xml")
    #print getFriendIds(xmldoc)
    xmldoc=minidom.parse("test/isAppAdded.xml")
    #print isAppAdded(xmldoc)
        
def testcode():
    u=User()
    u["hometown_location"]="abcde"
    print u["hometown_location"]
    print u.hometown_location

def testGetUserInfo():
    xmldoc=minidom.parse("test/getInfo.xml")
    fields=[profilefield.NAME,profilefield.SEX,
        profilefield.BIRTHDAY,profilefield.HS_HISTORY,profilefield.TINYURL,
        profilefield.HEADURL,profilefield.MAINURL,
        profilefield.HOMETOWN_LOCATION,profilefield.WORK_HISTORY,
        profilefield.UNIVERSITY_HISTORY]
    fields=[profilefield.NAME,profilefield.SEX,
            profilefield.BIRTHDAY,profilefield.TINYURL,
            profilefield.HEADURL,profilefield.MAINURL,
            profilefield.HOMETOWN_LOCATION]
    s=set(fields)
    fields=list(s)
    fields.sort(lambda x,y:cmp(x._order,y._order))
    us=getUserInfo(xmldoc,fields)
    #for u in us:
        #print u.toString()
    
if __name__=="__main__":
#    xmldoc=minidom.parse("test/getFriends.xml")
#    friends=getFriends(xmldoc)
#    for f in friends:
#        print f.id,f.name,f.headurl
    testGetUserInfo()